require'open-uri'
require'cgi'
class XiaoI
  Webbot_Path="http://202.109.73.87/engine/flashrobot2/webbot.js"
  Send_Path="http://202.109.73.87/engine/flashrobot2/send.js"
  Recv_Path="http://202.109.73.87/engine/flashrobot2/recv.js"
  def initialize()
    @sid=open(Webbot_Path).read.scan(/sessionId\s?=\s?"(\d+)"/m).flatten.to_s
    open(Send_Path + "?SID=#{@sid}&USR=#{@sid}&CMD=JOIN&r=")
    open(Recv_Path + "?SID=#{@sid}&USR=#{@sid}&r=")
    @sid
  end
  def send(msg='')
    open(Send_Path + "?SID=#{@sid}&USR=#{@sid}&CMD=CHAT&SIG=You&MSG=#{CGI::escape(msg)}")
  end
  def recv
    open(Recv_Path + "?SID=#{@sid}&USR=#{@sid}&r=").read.scan(/\(\[\{"MSG":"(.*?)"/).flatten.to_s
  end
 end
 